package org.springblade.modules.kafka.handler;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.flume.entity.TestFlumeData;
import org.springblade.modules.flume.entity.UatSdkData;
import org.springblade.modules.flume.service.ITestFlumeDataService;
import org.springblade.modules.gather.entity.GatherData;
import org.springblade.modules.gather.service.IGatherDataService;
import org.springblade.modules.kafka.constant.KafkaConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

/**
 * 消息处理器
 *
 * @author
 */
@Slf4j
@Component
//@AllArgsConstructor
public class MessageHandler {

	@Autowired
	private IGatherDataService gatherDataService;

	@Autowired
	private ITestFlumeDataService testFlumeDataService;

	@KafkaListener(topics = KafkaConstant.TOPIC_GATHER, containerFactory = "ackContainerFactory")
	public void handleMessage(ConsumerRecord record, Acknowledgment acknowledgment) {
		try {
			String message = (String) record.value();
			log.info("收到消息: {}", message);

			TestFlumeData flumeData = new TestFlumeData();
			flumeData.setGatherData(message);
			flumeData.setCreateTime(DateUtil.now());
			testFlumeDataService.save(flumeData);

		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage(), e);
		} finally {
			// 手动提交 offset
			acknowledgment.acknowledge();
		}
	}
}
